<template>
  <div class="settings">
    <van-nav-bar safe-area-inset-top placeholder :title="$t('BasicSetting')" left-arrow fixed @click-left="onClickLeft" />
    <van-pull-refresh v-model="isLoading" :pulling-text="$t('PullingText')" :loosing-text="$t('PullingText')" :success-text="$t('refreshSuccee')" @refresh="onRefresh">
      <van-collapse class="marginTop15" v-model="activeNames">
        <van-collapse-item :title="$t('Datetime')" name="1">
          <datetimePTVue :times="LocalArr.datetimes"></datetimePTVue>
        </van-collapse-item>
        <van-collapse-item :title="$t('Language')" name="2">
          <van-field v-model="LocalArr.lang" clickable readonly @click="showLanguage = true">
              <template #button>
                <van-button size="small" round @click="setLang($event)" class="setbtn">{{$t('Save')}}</van-button>
              </template>
          </van-field>
          <van-popup v-model="showLanguage" round position="bottom">
            <van-picker show-toolbar 
              :columns="langArr" 
              @cancel="showLanguage = false" 
              @confirm="LanguageConfirm"
              :confirm-button-text="$t('OK')"
              :cancel-button-text="$t('Cancel')" 
            />
          </van-popup>
        </van-collapse-item>
      </van-collapse>
    </van-pull-refresh>
  </div>
</template>

<script>
import datetimePTVue from '../../../components/DateTime/BDDateTimePT'
import languagePTVue from '../../../components/Language/BDLanguagePT'
export default {
  components:{
    datetimePTVue,
    languagePTVue
  },
  data(){
    return {
      isLoading:false,
      activeNames:[],
      showLanguage:false,
      langCode:'',
      langArr:[{text:this.$t('English'),code:0},{text:this.$t('Portuguese'),code:4}],
      LocalArr:{
        datetimes:'',
        lang:''
      }
    }
  },
  methods: {
    onClickLeft(){
      this.$router.push({path:'/bdsetting'});
    },
    LanguageConfirm(value){
      this.showLanguage=false;
      this.LocalArr.lang=value.text;
      this.langCode=value.code;
    },
    async getData(){
      this.comm.loadingShow();
      let datas = await this.SETAPI.LocalIn({'optType':'ReadSetData','pwd':sessionStorage.getItem('bdPwd')});
      if(datas!=null||datas!=undefined){
        let dataArr = datas;
        let year=dataArr[29];
        let month=dataArr[28]>=10?dataArr[28]:"0"+dataArr[28];
        let day=dataArr[27]>=10?dataArr[27]:"0"+dataArr[27];
        let hour=dataArr[26]>=10?dataArr[26]:"0"+dataArr[26];
        let minute=dataArr[25]>=10?dataArr[25]:"0"+dataArr[25];
        this.LocalArr.datetimes=`${year+2000}-${month}-${day} ${hour}:${minute}`;
        this.langCode=dataArr[67];
        if(this.langCode==0){
          this.LocalArr.lang=this.langArr[0].text;
        }else{
          this.LocalArr.lang=this.langArr[1].text;
        }
      }else{
        this.comm.tipsShow('InvNoResponse');
      }
    },
    async setLang(e){
      e.stopPropagation();
      //处理数据
      let params={
        'optType':'setReg',
        'data':JSON.stringify({'num':1,'Data':[{'reg':71,'val':this.langCode+''}]}),
        'pwd':sessionStorage.getItem('bdPwd')
      };
      this.comm.loadingShow();
      let data = await this.SETAPI.LocalIn(params);
      let newdata = data.substring(0, 1);
      if (newdata == "Y") {
        this.comm.toastSuccess();
      }else {
        this.comm.toastFail();
      }
    },
    onRefresh(){
      this.isLoading = false;
      for(let i in this.LocalArr){
        this.LocalArr[i]='';
      }
      this.getData();
    }
  },
  mounted(){
    setTimeout(()=>{
      this.getData();
    },1000);
  }
}
</script>

<style scoped lang="less">

</style>
